package com.hr.dto;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
public class JsonResp  //构建json返回数据的data
{
    private int code =400;
    private String msg ="badrequest";
    private Map<String, Object> data=new HashMap<>();

    public JsonResp(HttpStatus code) {
        this.code = code.value();
        this.msg = code.getReasonPhrase();
    }

    public JsonResp(HttpStatus code, Map<String, Object> data) {
        this.code = code.value();
        this.msg = code.getReasonPhrase();
        this.data = data;
    }

    public JsonResp(HttpStatus code, String msg) {
        this.code = code.value();
        this.msg =msg;
    }

    public static JsonResp ok(String s, String key, Object value)
    {
        JsonResp jsonResp=JsonResp.ok(s);
        jsonResp.addData(key, value);
        return jsonResp;
    }

    public static JsonResp ok(String key, Object value)
    {
        JsonResp jsonResp=JsonResp.ok("menus");
        jsonResp.addData(key, value);
        return jsonResp;
    }

    /**
     * 添加一对key-Value到该json响应结果中
     * @param key json字段名
     * @param value 该字段的数据
     * @return
     */
    public JsonResp addData(String key, Object value)
    {
        data.put(key,value);
        return this;
    }

    /**
     * 返回badrequest默认模板
     * @param msg the message string to be logged
     */
    public static JsonResp badRequest(String msg)
    {
        return new JsonResp(HttpStatus.BAD_REQUEST,msg);
    }

    /**
     * Lo返回ok默认模板
     *
     */
    public static JsonResp ok()
    {
        return new JsonResp(HttpStatus.OK);
    }

    public static JsonResp ok(String s)
    {
        return new JsonResp(HttpStatus.OK, s);
    }
}
